package cn.fl.spl.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.constant.WebBaseConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.spl.service.ICstRelInfoViewService;
import cn.fl.spl.vo.CstRelInfoViewVO;
import com.alibaba.fastjson.JSONObject;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.spl.facade.framework.constant.DictionaryConstant;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.view.condition.CstRelInfoViewQC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Description: 证件号查重 控制层
 */
@Controller
@RequestMapping("/spl/cstRelInfoView")
public class CstRelInfoViewController extends BaseController {

	/**
	 * CstRelInfoView
	 */
	@Autowired
	private ICstRelInfoViewService cstRelInfoViewService;
	/**
	 * 数据字典
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	/**
	 * 到查重页面
	 */
	@RequestMapping(value = "/toCstRelInfoView.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toCstRelInfoView(HttpServletRequest request, HttpServletResponse response) {
		String certNo = request.getParameter("certNo");

		ModelAndView view = new ModelAndView("spl/cstRelInfo/cstRelInfoView");
		
		Map<String, String> propTypCdMap = this.sysDictionaryApiService. // 查询客户类型
				searchGroupToMap(WebBaseConstant.SYS_SYS_CD, WebBaseConstant.SYS_P_ORG_CD, DictionaryConstant.SPL_ATTR_CD);
		BaseLogUtils.newWebLogger("客户类型数据字典->" + JSONObject.toJSONString(propTypCdMap))
				.setLogOperType(LogOperEnum.RESP)
				.setSysCd(CurrentThreadContext.getCurrentSysCd())
				.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
				.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
				.setWarning(false)
				.info();

		Map<String, String> relTypCdMap = this.sysDictionaryApiService. // 查询客户关系类型
				searchGroupToMap(WebBaseConstant.SYS_SYS_CD, WebBaseConstant.SYS_P_ORG_CD, DictionaryConstant.CST_REL_TYP_CD);
		BaseLogUtils.newWebLogger("客户关系类型数据字典->" + JSONObject.toJSONString(relTypCdMap))
				.setLogOperType(LogOperEnum.RESP)
				.setSysCd(CurrentThreadContext.getCurrentSysCd())
				.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
				.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
				.setWarning(false)
				.info();

		view.addObject("certNo", certNo);
		view.addObject("relTypCdMap", relTypCdMap);
		view.addObject("propTypCdMap", propTypCdMap);

		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {

		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String certNo = request.getParameter("certNo");// 证件号
		String relNm = request.getParameter("relNm");// 关联名称
		String relTypCd = request.getParameter("relTypCd");
		String propTypCd = request.getParameter("propTypCd");
		
		String sysCd = WebBaseConstant.SYS_SYS_CD;
		String orgCd = WebBaseConstant.SYS_P_ORG_CD;
		String chlCd = "CHL001";

		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		CstRelInfoViewQC condition = new CstRelInfoViewQC();
		condition.setIdNum(certNo);
		if (StringUtils.isNotEmpty(relNm)) {
			condition.setRelNm(relNm);
		}
		if (StringUtils.isNotEmpty(relTypCd)) {
			condition.setRelTypCd(relTypCd);
		}
		if (StringUtils.isNotEmpty(propTypCd)) {
			condition.setPropTypCd(Integer.valueOf(propTypCd));
		}
		
		page.setCondition(condition);
		
		Pager pager = this.cstRelInfoViewService.searchListPage(page, sysCd, orgCd, chlCd);
		if (null == pager) {
			pager = new Pager();
		}
		
		List<CstRelInfoViewVO> list = (List<CstRelInfoViewVO>) pager.getResultList();
		BaseLogUtils.newWebLogger("查询结果=>" + JSONObject.toJSONString(list))
				.setLogOperType(LogOperEnum.RESP)
				.setSysCd(CurrentThreadContext.getCurrentSysCd())
				.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
				.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
				.setWarning(false)
				.info();

		ListPageVO<CstRelInfoViewVO> listPage = new ListPageVO<CstRelInfoViewVO>(list, pager.getTotalCount());
		return listPage;
	}


	

	/**
	 * 到新增修改页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView view = new ModelAndView("spl/splVisitRec/splVisitRecAdd");

		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {

		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("spl/splVisitRec/splVisitRecModify");
		view.addObject("id", id);
		return view;
	}

	/**
	 * 到详情页面
	 */
	@RequestMapping(value = "/toDetailPage.do", method = { RequestMethod.GET })
	public ModelAndView toDetailPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("spl/splVisitRec/splVisitRecDetail");
		view.addObject("id", id);

		return view;
	}
}
